import { createApp } from 'vue'
import Toast from './Toast.vue'

interface ToastOptions {
  msg: string;
  type?:string;
  duration?: number;
  mask?: boolean;
}
const toast= {
  instance: null as any,
  parent: document.createElement('div'),
  showToast({ msg, duration = 5000, mask = false ,type="success"}: ToastOptions) {
    if (this.instance !== null) this.clear() 
    this.instance = createApp(Toast, { msg: msg.toString(), mask,type })
    document.body.appendChild(this.parent)
    this.instance.mount(this.parent)
    setTimeout(() => {
      this.clear()
    }, duration)
  },
  hideToast() {
    setTimeout(() => {
      this.clear()
    }, 500)
    
  },
  clear() {
    if (this.instance !== null) {
      this.instance.unmount(this.parent)
      document.body.removeChild(this.parent)
      this.instance = null
    }
  },
}

export default toast
